package bc.swim2.web.servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bc.swim2.ejb.entity.Friendship;
import bc.swim2.ejb.entity.RegisteredUser;
import bc.swim2.ejb.session.FriendshipManagerBeanRemote;
import bc.swim2.ejb.session.UserManagerBeanRemote;

public class FriendshipServlet extends HttpServlet {

	public FriendshipServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	private static final long serialVersionUID = -947498875066169733L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("methodName").equals("showSuggests")) {
			showSuggests(request, response);
		} else if (request.getParameter("methodName").equals(
				"visualizedNewFriend")) {
			visualizedNewFriend(request, response);
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		if (request.getParameter("methodName").equals("sendFriendshipRequest")) {
			sendFriendshipRequest(request, response);
		} else if (request.getParameter("methodName").equals(
				"indirectSendFriendshipRequest")) {
			indirectSendFriendshipRequest(request, response);
		} else if (request.getParameter("methodName").equals("approve")) {
			approveFriendship(request, response);
		} else if (request.getParameter("methodName").equals("decline")) {
			declineFriendship(request, response);
		}

	}

	private void showSuggests(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref2 = ctx.lookup("FriendshipManagerBean/remote");
			FriendshipManagerBeanRemote fm = (FriendshipManagerBeanRemote) ref2;

			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			int accFrId = Integer.parseInt(request
					.getParameter("acceptedFriendId"));
			RegisteredUser accFr = um.getUserById(accFrId);
			RegisteredUser curr = (RegisteredUser) session
					.getAttribute("currentSessionUser");

			Friendship f = fm.getFriendship(curr, accFr);
			fm.setVisualizedTrue(f.getId());

			session.setAttribute("accFrIdTmp", accFrId);
			response.sendRedirect("MainServlet?to=FriendsOfFriends");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void visualizedNewFriend(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref2 = ctx.lookup("FriendshipManagerBean/remote");
			FriendshipManagerBeanRemote fm = (FriendshipManagerBeanRemote) ref2;

			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			int accFrId = Integer.parseInt(request
					.getParameter("acceptedFriendId"));
			RegisteredUser accFr = um.getUserById(accFrId);
			RegisteredUser curr = (RegisteredUser) session
					.getAttribute("currentSessionUser");

			Friendship f = fm.getFriendship(curr, accFr);
			fm.setVisualizedTrue(f.getId());

			response.sendRedirect("MainServlet?to=RegisteredUserHome");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void sendFriendshipRequest(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref2 = ctx.lookup("FriendshipManagerBean/remote");
			FriendshipManagerBeanRemote fm = (FriendshipManagerBeanRemote) ref2;
			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			RegisteredUser sender = (RegisteredUser) session
					.getAttribute("currentSessionUser");
			RegisteredUser receiver = (RegisteredUser) um.getUserById(Integer
					.parseInt(request.getParameter("userId")));

			fm.sendFriendshipRequest(sender, receiver, false);

			response.sendRedirect("MainServlet?to=SearchHelpAndUsersResult");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void indirectSendFriendshipRequest(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref2 = ctx.lookup("FriendshipManagerBean/remote");
			FriendshipManagerBeanRemote fm = (FriendshipManagerBeanRemote) ref2;
			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			RegisteredUser sender = (RegisteredUser) session
					.getAttribute("currentSessionUser");
			RegisteredUser receiver = (RegisteredUser) um.getUserById(Integer
					.parseInt(request.getParameter("userId")));

			fm.sendFriendshipRequest(sender, receiver, true);

			response.sendRedirect("MainServlet?to=FriendsOfFriends");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void approveFriendship(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref3 = ctx.lookup("FriendshipManagerBean/remote");
			FriendshipManagerBeanRemote fm = (FriendshipManagerBeanRemote) ref3;
			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			RegisteredUser rur = (RegisteredUser) session
					.getAttribute("currentSessionUser");
			int uid = Integer.parseInt(request.getParameter("userId"));
			RegisteredUser rus = um.getUserById(uid);
			session.setAttribute("accFrIdTmp", uid);

			fm.approveFriendshipRequest(rus, rur);
			Friendship f = fm.getFriendship(rus, rur);

			if (f.getIndirect()) {
				response.sendRedirect("MainServlet?to=FriendshipRequests");
			} else {
				response.sendRedirect("MainServlet?to=FriendsOfFriends");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void declineFriendship(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			HttpSession session = request.getSession(true);
			Context ctx = (Context) session.getAttribute("ctx");

			Object ref3 = ctx.lookup("FriendshipManagerBean/remote");
			FriendshipManagerBeanRemote fm = (FriendshipManagerBeanRemote) ref3;
			Object ref4 = ctx.lookup("UserManagerBean/remote");
			UserManagerBeanRemote um = (UserManagerBeanRemote) ref4;

			RegisteredUser rur = (RegisteredUser) session
					.getAttribute("currentSessionUser");

			int uid = Integer.parseInt(request.getParameter("userId"));
			RegisteredUser rus = um.getUserById(uid);

			fm.declineFriendshipRequest(rus, rur);

			response.sendRedirect("MainServlet?to=FriendshipRequests");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
